package cn.akwangl.future;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.*;

/**
 * 批量获取Future 简单用法.
 *
 * @author 阿坤同学
 * @date 2022-12-13 10:24
 */
public class SimpleBatchFuture {

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        OrderNoTask orderNoTask = new OrderNoTask();

        List<Future<Integer>> futures = new ArrayList();
        for (int i = 0; i < 20; i++) {
            Future<Integer> orderNoFuture = executorService.submit(orderNoTask);
            futures.add(orderNoFuture);
        }

        try {
            for (int i = 0; i < futures.size(); i++) {
                Future<Integer> orderNoFuture = futures.get(i);
                Integer orderNo = orderNoFuture.get();
                System.out.println("i=" + i + "orderNo=" + orderNo);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        executorService.shutdown();
    }

    static class OrderNoTask implements Callable<Integer> {
        public static final Random random = new Random();

        @Override
        public Integer call() throws Exception {
            return random.nextInt();
        }
    }
}
